<?php
include_once($_SERVER["DOCUMENT_ROOT"] . "/model/base_function.php");

function model_get_news_list($type, $page, $list_per_page)
{
	$news_list = array();

	$connection = connect_to_database();
	$query_string = "select * from news";
	if ($type)
	{
		$query_string = $query_string . " WHERE type_id = " . $type;
	}

	$query_string = $query_string . " ORDER BY id";
	$query_string = $query_string . " LIMIT ". ($page - 1) * $list_per_page . ", " . $list_per_page;

	$result = mysql_query($query_string, $connection);

	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$news = $row;
		// 详细信息页面地址
		$news["detail_view_url"] = "details.php?id=". $news["id"]; 
		$news_list[] = $news;
	}
	
	mysql_close($connection);
	return $news_list;
}

function model_get_news_page_count($type, $list_per_page)
{
	$connection = connect_to_database();

	$query_string = "select count(*) from news";
	if ($type)
	{
		$query_string = $query_string . " WHERE type_id = " . $type;
	}
	$result = mysql_query($query_string, $connection);
	$row = mysql_fetch_row($result);
	$result_count = $row[0];

	$page_count = $result_count / $list_per_page;
	if ($result_count % $list_per_page > 0)
	{
		$page_count = $page_count + 1;
	}
	return $page_count;
}

function model_get_news_details($news_id)
{
	$news_details = array();
	$connection = connect_to_database();

	$query_string = "select * from news where id = " . $news_id;
	$result = mysql_query($query_string, $connection);
	if (!$result)
	{
		return;
	}

	$news_details = mysql_fetch_array($result, MYSQL_ASSOC);
	mysql_free_result($result);

	mysql_close($connection);
	return $news_details;
}

function model_news_count_click($news_id)
{
	$news_details = array();
	$connection = connect_to_database();

	$query_string =
	"UPDATE news SET click_count = click_count + 1 WHERE id = " . $news_id;
	if (mysql_query($query_string, $connection) == true)
	{
		mysql_close($connection);
		return true;
	}
	else
	{
		mysql_close($connection);
		return false;
	}
}

function model_insert_news($news)
{
	$connection = connect_to_database();

	// sql for upload news
	$query_string =
	"INSERT
		INTO news
		SET
			title = \"". mysql_real_escape_string($news["title"], $connection)  ."\",
			type_id = " . mysql_real_escape_string($news["type"], $connection)  . ",
			content = \"" . mysql_real_escape_string($news["content"], $connection) . "\",
			publish_time = NOW();";

	$result = mysql_query($query_string, $connection);
	if ($result == NULL)
	{
		print(mysql_error());
		mysql_close($connection);
		return false;
	}

	mysql_close($connection);
	return true;
}

function model_delete_news($id)
{
	$news_details = array();
	$connection = connect_to_database();

	$query_string = "DELETE FROM news where id = " . $id;
	if (mysql_query($query_string, $connection) == true)
	{
		mysql_close($connection);
		return true;
	}
	else
	{
		mysql_close($connection);
		return false;
	}
}

function model_update_news($id, $news)
{
	$connection = connect_to_database();

	// sql for upload news
	$query_string =
	"UPDATE news
		SET
			title = \"". mysql_real_escape_string($news["title"], $connection)  ."\",
			type_id = " . mysql_real_escape_string($news["type"], $connection)  . ",
			content = \"" . mysql_real_escape_string($news["content"], $connection) . "\",
			publish_time = NOW()
		WHERE id = " . $news["id"];

	$result = mysql_query($query_string, $connection);
	if ($result == NULL)
	{
		print(mysql_error());
		mysql_close($connection);
		return false;
	}

	mysql_close($connection);
	return true;
}
?>
